package com.lcz.wanandroid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lcz.wanandroid.R;
import com.lcz.wanandroid.activity.Serac.SeracBean;
import com.lcz.wanandroid.activity.Serac.SeracP;
import com.lcz.wanandroid.activity.Serac.SeracV;
import com.lcz.wanandroid.adapter.SearAdapter;
import com.lcz.wanandroid.base.BaseActivity;
import com.lcz.wanandroid.base.SearchBean;
import com.lcz.wanandroid.utils.FlowLayout;
import com.lcz.wanandroid.utils.SeracUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearcActivity extends BaseActivity<SeracV, SeracP> implements View.OnClickListener, SeracV {

    @BindView(R.id.serac_rlv)
    FlowLayout seracRlv;
    @BindView(R.id.searc_ed)
    EditText searcEd;
    @BindView(R.id.serlv)
    RecyclerView serlv;
    @BindView(R.id.has_some)
    TextView hasSome;
    @BindView(R.id.ll)
    LinearLayout ll;
    private ImageView mSearReturn;
    private Toolbar mSeracToobar;

    /**
     * 搜索
     */
    private Button mSercBtn;
    private ArrayList<SearchBean> mList;
    private SearAdapter mSearAdapter;
    private List<SearchBean> mSearchBeans;


    @Override
    protected SeracP Inpresneter() {
        return new SeracP();
    }

    @Override
    protected void initView() {
        mSearReturn = (ImageView) findViewById(R.id.sear_return);
        mSeracToobar = (Toolbar) findViewById(R.id.serac_toobar);
        mSeracToobar.setTitle("");
        setSupportActionBar(mSeracToobar);
        mSercBtn = (Button) findViewById(R.id.serc_btn);
        mSercBtn.setOnClickListener(this);

        mSearReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mList = new ArrayList<>();
        mSearchBeans = SeracUtils.getmSeracUtils().QueryAll();
        mSearAdapter = new SearAdapter(mSearchBeans, this);
        serlv.setAdapter(mSearAdapter);

        serlv.setLayoutManager(new LinearLayoutManager(this));

        if (mSearchBeans.size() > 0) {
            hasSome.setVisibility(View.INVISIBLE);
            serlv.setVisibility(View.VISIBLE);
        } else {
            serlv.setVisibility(View.INVISIBLE);
            hasSome.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        mpresenter.setData();
    }

    @Override
    protected int LayoutId() {
        return R.layout.activity_searc;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.serc_btn:
                String trim = searcEd.getText().toString().trim();
                if (trim != null) {
                    SearchBean searchBean = new SearchBean();
                    searchBean.setId(null);
                    searchBean.setTitle(trim);
                    mList.add(searchBean);
                    SeracUtils.getmSeracUtils().insert(searchBean);

                    mSearAdapter.setList(mList);
                    mSearAdapter.notifyDataSetChanged();
                    serlv.setVisibility(View.VISIBLE);
                    hasSome.setVisibility(View.INVISIBLE);

                    Intent intent = new Intent(SearcActivity.this, NoCellectWebActivity.class);
                    intent.putExtra("url", "");
                    intent.putExtra("name", trim);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void onSussion(final SeracBean bean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final List<SeracBean.DataBean> data = bean.getData();
                for (int i = 0; i < data.size(); i++) {
                    String name = data.get(i).getName();
                    TextView textv = (TextView) LayoutInflater.from(SearcActivity.this).inflate(R.layout.layout_tv, null);
                    textv.setTop(18);
                    if (i % 3 == 0) {
                        textv.setBackground(getResources().getDrawable(R.drawable.shape_sear));
                    } else if (i % 2 == 0) {
                        textv.setBackground(getResources().getDrawable(R.drawable.shape_redddd));
                    }
                    textv.setText(name);
                    seracRlv.addView(textv);

                    final int finalI = i;
                    textv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String link = data.get(finalI).getLink();
                            Intent intent = new Intent(SearcActivity.this, NoCellectWebActivity.class);
                            intent.putExtra("url", link);
                            intent.putExtra("name", data.get(finalI).getName());
                            startActivity(intent);

                            SearchBean searchBean = new SearchBean();
                            searchBean.setId(null);
                            searchBean.setTitle(data.get(finalI).getName());
                            SeracUtils.getmSeracUtils().insert(searchBean);
                            mList.add(searchBean);
                            mSearAdapter.setList(mList);
                            mSearAdapter.notifyDataSetChanged();
                            serlv.setVisibility(View.VISIBLE);
                            hasSome.setVisibility(View.INVISIBLE);
                        }
                    });
                }
            }
        });
    }


    @OnClick(R.id.ll)
    public void onViewClicked() {
        mList.clear();
        SeracUtils.getmSeracUtils().DeleteAll();
        mSearAdapter.setList(mList);
        mSearAdapter.notifyDataSetChanged();
        hasSome.setVisibility(View.VISIBLE);
    }
}
